package com.travel.server.manager.impl.command.order.data;

import com.travel.common.excel.ExcelData;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.ExcelUtil;
import com.travel.server.manager.dto.request.order.data.ExportSaleTargetListRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.dto.response.order.data.QuerySaleTargetListResponse;
import com.travel.server.manager.dto.response.order.data.QuerySaleTargetListResponse.TargetItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/7 16:32
 * @Desc:
 */
@Component
@Slf4j
public class ExportSaleTargetListCommand extends AbstractCommand<ExportSaleTargetListRequest, EmptyResponse> {

    @Autowired
    private QuerySaleTargetListCommand querySaleTargetListCommand;

    @Override
    protected void checkInput(ExportSaleTargetListRequest request) {
        checkTimeInput(request);
    }

    @Override
    public EmptyResponse handle(ExportSaleTargetListRequest request) {
        checkInput(request);
        List<TargetItem> targetList = querySaleTargetListCommand.makeTargetList(request);
        if(CollectionUtil.isEmpty(targetList)){
            throw new WorkException(WorkCode.EXCEL_DATA_NULL);
        }

        List<String> title = Arrays.asList("项目名称", "总订单数", "退款单数", "转接单数", "累计成单数", "订单负责人");
        List<List<Object>> rows = new ArrayList<>();
        for(TargetItem item : targetList){
            List<Object> objects = new ArrayList<>();
            objects.add(item.getProject());
            objects.add(item.getTotal());
            objects.add(item.getRefund());
            objects.add(item.getTotal());
            objects.add(item.getOrder());
            objects.add(item.getOrderCharge());
            rows.add(objects);
        }
        ExcelData excelData = new ExcelData();
        excelData.setName("项目销售统计");
        excelData.setTitles(title);
        excelData.setRows(rows);

        ExcelUtil.exportExcel(getHttpServletResponse(), "项目销售统计", excelData);
        return new EmptyResponse();
    }
}
